package com.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.app.model.Fwlx;
import com.app.service.IFwlxService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 房屋类型 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2025-02-28
 */
@Api(tags = "房屋类型管理")
@RestController
@RequestMapping("/fwlx")
public class FwlxController {

    @Autowired
    private IFwlxService fwlxService;

    @ApiOperation(value = "获取房屋类型列表", notes = "获取房屋类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer")
    })
    @GetMapping("/adminGetFwlxList")
    private Result adminGetFwlxList(Integer page, Integer limit) {
        Page<Fwlx> pageList = new Page<>(page, limit);

        QueryWrapper<Fwlx> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

        Page<Fwlx> fwlxPage = fwlxService.page(pageList, queryWrapper);

        JSONObject result = new JSONObject();
        result.put("total", fwlxPage.getTotal());
        result.put("items", fwlxPage.getRecords());
        return Result.success(result);
    }

    @ApiOperation(value = "获取房屋类型列表", notes = "获取房屋类型列表")
    @GetMapping("/adminGetFwlx")
    private Result adminGetFwlx() {

        QueryWrapper<Fwlx> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

        List<Fwlx> fwlxPage = fwlxService.list(queryWrapper);

        return Result.success(fwlxPage);
    }

    @ApiOperation(value = "保存房屋类型", notes = "保存房屋类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fwlx", value = "房屋类型对象", required = true, dataType = "Fwlx")
    })
    @PostMapping("/adminSaveFwlx")
    public Result adminSaveFwlx(@RequestBody Fwlx fwlx) {
        if (fwlx.getId() == null) {
            fwlx.setStatus(1);
            fwlxService.save(fwlx);
        }else {
            fwlxService.updateById(fwlx);
        }
        return Result.success("操作成功！");
    }

}

